package com.droidathletics.boundary.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.droidathletics.boundary.dao.SportsmanDAOAdapter.SportsmanDBOpenHelper;

public class CompetitionDAOAdapter {
	private static final String DATABASE_NAME = "droidathleticsdb.db";
	private static final int DATABASE_VERSION = 1;
	
	private static final String COMPETITION_TABLE_NAME = "competitions";
    private static final String COMPETITION_TABLE_CREATE =
        "CREATE TABLE " + COMPETITION_TABLE_NAME + " (name TEXT, date TEXT, " +
        		"place TEXT, numbersportsman INTEGER, PRIMARY KEY(name))";
    private static final String COMPETITION_TABLE_READ =
    	"SELECT * FROM " + COMPETITION_TABLE_NAME;
	
    private SQLiteDatabase database;
	private final Context context;
    private CompetitionDBOpenHelper competitionHelper;
    
	public CompetitionDAOAdapter(Context context) {
		this.context = context;
		competitionHelper = new CompetitionDBOpenHelper(this.context);
	    this.database = competitionHelper.getWritableDatabase();
	}
	
	public static class CompetitionDBOpenHelper extends SQLiteOpenHelper {
		
		public CompetitionDBOpenHelper(Context context) 
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) 
		{
			db.execSQL(COMPETITION_TABLE_CREATE);	
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			db.execSQL("DROP TABLE IF EXISTS " + COMPETITION_TABLE_NAME);
			onCreate(db);	
		}
	}

    public void close() {
        competitionHelper.close();
    }
    
    public void deleteTable()
    {
    	this.database.delete(COMPETITION_TABLE_NAME, null, null);
    }
    
    public long createCompetition(String nameCompetition, String day, String month, String year,
            String placeCompetition, String sportsmansParticipating) {
        
    	long result = -1;
    	
    	ContentValues values = new ContentValues();
        values.put("name", nameCompetition);
        values.put("date", day+"/"+month+"/"+year);
        values.put("place", placeCompetition);
        values.put("numbersportsman", Integer.parseInt(sportsmansParticipating));
        
        try {
            result = database.insertOrThrow(COMPETITION_TABLE_NAME, null, values);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public long updateCompetition(String nameCompetition, String day, String month, String year,
            String placeCompetition, String sportsmansParticipating) {
        
    	long result = -1;
    	
    	ContentValues values = new ContentValues();
        values.put("name", nameCompetition);
        values.put("date", day+"/"+month+"/"+year);
        values.put("place", placeCompetition);
        values.put("numbersportsman", Integer.parseInt(sportsmansParticipating));
        
        try {
            result = database.update(COMPETITION_TABLE_NAME, values, 
            		      "name='"+nameCompetition+"'", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    public long deleteCompetition(String name){
        long result = -1;
        
    	try {
            result = database.delete(COMPETITION_TABLE_NAME, "name='"+name+"'", null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return result;
    	
    }
    
   public Cursor readCompetition(){
	   return database.query(COMPETITION_TABLE_NAME, new String[] {
				"name", "date", "place", "numbersportsman"}, null, null, null,
				null, null);
   
   }
   
   public Cursor searchCompetition(String pam1){
	   
	   String query = "name like " + "'" + pam1 + "%'" + " or place like " + "'" + pam1 + "%'" 
			   + " or date like " + "'" + pam1 + "'";
	   
	   return database.query(COMPETITION_TABLE_NAME, new String[] {
				"name", "date", "place", "numbersportsman"}, query, null, null,
				null, null);
   }

public Cursor searchDateCompetition(String pam) {
	
	String query = "date like " + "'" + pam + "'";
	   
	   return database.query(COMPETITION_TABLE_NAME, new String[] {
				"name", "date", "place", "numbersportsman"}, query, null, null,
				null, null);
}
}
